<template>

    <div class="login1">
      <el-form
          v-loading="loading"
          element-loading-text="正在登录..."
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.8)"
          :rules="rules"
          ref="loginForm"
          :model="loginForm"
          class="loginContainer1"
      >
        <h3 class="loginTitle">欢迎登录QR二维码</h3>

        <el-form-item prop="username">
          <el-input
              type="text"
              auto-complete="false"
              v-model="loginForm.username"
              placeholder="请输入用户名"
              class="input1"
          ></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
              type="password"
              auto-complete="false"
              v-model="loginForm.password"
              placeholder="请输入密码"
              class="input1"
          ></el-input>
        </el-form-item>

        <el-form-item prop="code">
          <el-input
              size="normal"
              type="text"
              auto-complete="false"
              v-model="loginForm.code"
              placeholder="点击图片更换验证码"
              style="width: 250px; margin-right: 5px;float:left"
              class="input1"
          ></el-input>
          <img :src="captchaUrl" @click="updateCaptcha" style="float: right" />

        </el-form-item>

        <el-button class="loginButton"  type="primary" @click="submitLogin" @keyup.enter="submitLogin"
        >登录</el-button
        >
<!--        <el-button type="primary" style="right: 50%" @click="register">注册</el-button-->
<!--        >-->
        <el-form-item style="width: 100%">
          <a @click="register" class="register">注册</a>
        </el-form-item>
      </el-form>
    </div>

</template>

<script>
import {getRequest} from "@/utils/api";
import {Message} from "element-ui";

export default {
  name: "Login",
  data() {
    return {
      captchaUrl: "/captcha?time=" + new Date(),
      loginForm: {
        username: "",
        password: "",
        code: "",
      },
      loading: false,
      checked: true,
      rules: {
        username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        code: [{ required: true, message: "请输入验证码", trigger: "blur" }],
      },
    };
  },
  methods: {
    updateCaptcha() {
      this.captchaUrl = "/captcha?time=" + new Date();
    },
    register() {
      this.$router.push('/Register')
    },
    submitLogin() {
      this.loading = true;
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.postRequest('/login',this.loginForm).then(resp=>{
            if(resp?.code==200){
              this.loading = false;
              //存储用户token
              const tokenStr = resp.data.tokenHead+resp.data.token;
              window.sessionStorage.setItem("tokenStr", tokenStr);//08登录跳转有隐患，key值为NaN
              localStorage.setItem('tokenStr',tokenStr)
              getRequest('/manager/info').then(resp=>{

                window.sessionStorage.setItem("username", resp.username);
                window.sessionStorage.setItem("id", resp.id);
                if (resp?.auths=="admin") {
                  Message.success("登录成功")
                  this.$router.replace('/AdminPage');
                }else {
                  Message.success("欢迎您："+resp.username)
                this.$router.replace('/UserPage');}
              })
            }else {
              this.loading = false;
              // this.$message.error(resp?.message);
              this.updateCaptcha();
            }
          })
        } else {
          this.loading = false;
          this.$message.error("请输入全部信息！");
          return false;
        }
      });
    },
  },
};
</script>

<style >

.input1 {
  background: transparent!important;
}

.login1 {
  width: 100%;
  height: 100%;
  background: #3b498c url("../assets/login.jpg") no-repeat;
  background-size: cover;

}

.loginContainer1 {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 0px auto;
  transform: translateY(50%);
  width: 358px;
  padding: 15px 35px 15px 35px;
  background: rgba(90, 109, 185, 0.2);
}

.loginTitle {
  margin: 0px auto 40px auto;
  text-align: center;
  color: rgb(41, 49, 45);
}

.register {
  float: right;
  color: wheat;
}
.loginButton{
  width: 360px;
}
</style>
